public class PictureMessage extends Message {

    private boolean isDownloading;
    private String smallLocalPath="";
    private String murl="";

    public PictureMessage(TIMMessage message){
        this.message = message;
        TIMImageElem e = (TIMImageElem) message.getElement(0);
        if(message.status()== TIMMessageStatus.SendSucc){
            for(final TIMImage image : e.getImageList()) {
                if (image.getType() == TIMImageType.Thumb){
                    smallLocalPath=image.getUuid();
                }
                if (image.getType() == TIMImageType.Large){
                    murl=image.getUrl();
                }
            }
        }
    }

    public PictureMessage(String path){
        this(path, false);
    }

    
    public PictureMessage(String path,boolean isOri){
        message = new TIMMessage();
        TIMImageElem elem = new TIMImageElem();
        elem.setPath(path);
        elem.setLevel(isOri?0:1);
        message.addElement(elem);
    }

    public String getSmallLocalPath(){
        return smallLocalPath;
    }

    public String getmUrl(){
        return murl;
    }

    @Override
    public void showMessage(final ChatAdapter.ViewHolder viewHolder, final Context context) {
        clearView(viewHolder,true,context);
        if (checkRevoke(viewHolder)) return;
        TIMImageElem e = (TIMImageElem) message.getElement(0);
        switch (message.status()){
            case Sending:
                clearView(viewHolder,true,context);
                RelativeLayout.LayoutParams layoutParams=new RelativeLayout.LayoutParams(MyApplication.ChatImageviewS,MyApplication.ChatImageviewB);
                if(message.isSelf()){
                    layoutParams.setMargins(0,0,PxUtil.ChatImageviewMa,0);
                }else {
                    layoutParams.setMargins(PxUtil.ChatImageviewMa,0,0,0);
                }
                    RoundImageView imageView = new RoundImageView(MyApplication.getContext());
                    imageView.setLayoutParams(layoutParams);
                    Glide.with( context ).load( e.getPath() ).thumbnail( 0.05f ).into( imageView );
                    getBubbleView(viewHolder,true,context).addView(imageView);

                    RelativeLayout.LayoutParams layoutParams2=new RelativeLayout.LayoutParams(MyApplication.barWH,MyApplication.barWH);
                    layoutParams2.addRule(RelativeLayout.CENTER_IN_PARENT,RelativeLayout.TRUE);
                    layoutParams2.setMargins(0,0,MyApplication.ChatImageviewMa,0);
                    ProgressBar progressBar=new ProgressBar(MyApplication.getContext());
                    progressBar.setLayoutParams(layoutParams2);
                    progressBar.setIndeterminateDrawable(context.getResources().getDrawable(R.drawable.indeterminate_drawable));
                    getBubbleView(viewHolder,true,context).addView(progressBar);
                break;
            case SendSucc:
                for(final TIMImage image : e.getImageList()) {
                    if (image.getType() == TIMImageType.Thumb){
                        final String uuid = image.getUuid();
                        if (FileUtil.isCacheFileExist(uuid)){
                            showThumb(viewHolder,uuid,context);
                        }else{
                            image.getImage(FileUtil.getCacheFilePath(uuid), new TIMCallBack() {
                                @Override
                                public void onError(int code, String desc) {
                                }
                                @Override
                                public void onSuccess() {
                                    showThumb(viewHolder,uuid,context);
                                }
                            });
                        }
                    }
                  if (image.getType() == TIMImageType.Large){
                        getBubbleView(viewHolder,true,context).setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                murl=image.getUrl();
                                navToImageview();
                            }
                        });
                    }
                }
                break;
        }
        showStatus(viewHolder);
    }


    @Override
    public String getSummary() {
        String str = getRevokeSummary();
        if (str != null) return str;
        return MyApplication.getContext().getString(R.string.summary_image);
    }

    @Override
    public void save() {
        final TIMImageElem e = (TIMImageElem) message.getElement(0);
        for(TIMImage image : e.getImageList()) {
            if (image.getType() == TIMImageType.Original) {
                final String uuid = image.getUuid();
                if (FileUtil.isCacheFileExist(uuid+".jpg")) {
                    Toast.makeText(MyApplication.getContext(), MyApplication.getContext().getString(R.string.save_exist),Toast.LENGTH_SHORT).show();
                    return;
                }
                image.getImage(FileUtil.getCacheFilePath(uuid+".jpg"), new TIMCallBack() {
                    @Override
                    public void onError(int i, String s) {
                    }
                    @Override
                    public void onSuccess() {
                    }
                });
            }
        }
    }

    private void showThumb(final ChatAdapter.ViewHolder viewHolder,String filename,final Context context){
        smallLocalPath=filename;
        Bitmap bitmap = BitmapFactory.decodeFile(FileUtil.getCacheFilePath(filename));
        RoundImageView imageView = new RoundImageView(MyApplication.getContext());

        int oldw=bitmap.getWidth();
        int oldh=bitmap.getHeight();
        int newW=0;
        int newH=0;
        if(oldw>oldh){
            newH = PxUtil.ChatImageviewSizeH;
            newW=(PxUtil.ChatImageviewSizeH*oldw)/oldh;
        }else {
            newW=PxUtil.ChatImageviewSizew;
            newH=(PxUtil.ChatImageviewSizew*oldh)/oldw;
        }
        imageView.setImageBitmap(setImgSize(bitmap,newW,newH));
        RelativeLayout.LayoutParams layoutParams=new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        if(message.isSelf()){
            layoutParams.setMargins(0,0,PxUtil.ChatImageviewMa,0);
        }else {
            layoutParams.setMargins(PxUtil.ChatImageviewMa,0,0,0);
        }
        imageView.setLayoutParams(layoutParams);
        getBubbleView(viewHolder,true,context).addView(imageView);
    }

    private void navToImageview( ){
        ChatActivity.instance.updateImageList(smallLocalPath);
    }

}
